import { defineStore } from 'pinia'
import { ref } from 'vue'
import { checkLogin, getMenus } from '../services/administator/admin'
import { convertToTree } from '../utils/treeUtils'
import {useRouter} from 'vue-router'

export const useAuthStore = defineStore("auth", () => {

    const router=useRouter();
    const token = ref("");
    const menus = ref([]);

    const login = async (val: any) => {

        const { code, data, msg }: any = await checkLogin(val);
        if (code != 10000) {
            //提示
            return;
        }
        token.value = data.token;
        const mRes: any = await getMenus();
        console.log(222222,mRes);
        
        if (mRes.code === 10000) {
            let _menus = convertToTree(mRes.data.list.filter((item:any)=>item.isButton==0));

            console.log(11111111111,_menus);
            
            menus.value = _menus;
        }
        //跳转


        router.push("/company/list");





    }

    return {
        token, menus, login
    }


});